import datetime
import jwt
from django.conf import settings
from rest_framework.exceptions import AuthenticationFailed

key = settings.SECRET_KEY
# 生成token
class Myjwt:
    def gettoken(self, userid, account):
        user = {
            "id": userid,
            "username": account,
            "exp": datetime.datetime.now() + datetime.timedelta(hours=2)
        }
        return jwt.encode(user, key, algorithm="HS256")
    def checktoken(self, token):
        # 获取token是否存在
        if token:
            return jwt.decode(token, key, algorithms=['HS256'])
jwtweb = Myjwt()

